package com.jy.rhin.application.cda;

import com.jy.api.hospital.HospitalAPI;
import com.jy.api.hospital.dto.HospitalDTO;
import com.jy.common.StringUtil;
import com.jy.rhin.domain.cda.CDACreator;
import com.jy.rhin.facade.cda.dto.ChanHouFSDto;
import com.jy.rhin.infra.cda.CDAException;
import com.jy.rhin.infra.cda.service.ChanHouFSService;
import com.jy.rhin.infra.cda.ycf.ChanHouCFFSVo;
import com.jy.rhin.model.ChanHouFS;
import com.jy.rhin.model.ClinicalDocumentData;
import com.jy.rhin.model.CodeObject;
import com.jy.rhin.model.body.fuyou.*;
import com.jy.rhin.model.header.Author;
import com.jy.rhin.model.header.Custodian;
import com.jy.rhin.model.header.Patient;
import com.jy.rhin.model.header.PatientRole;
import com.jy.rhin.utils.CDAUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@Service
public class ChanHouFSApplication {

    @Resource
    private ChanHouFSService chanHouFSService;
    /*@Resource
    private DataMigrationAPI dataMigrationAPI;*/
    @Resource
    private CDACreator cdaCreator;

    @Resource
    private HospitalAPI hospitalAPI;

    public String create(ChanHouFSDto req) {
        return cdaCreator.create(createFS(req), "07chanhousf.ftl");
    }

    public ClinicalDocumentData createFS(ChanHouFSDto req) {
        ChanHouCFFSVo info = chanHouFSService.queryOne(req);
        if(Objects.isNull(info)){
            throw new CDAException("主业务数据不存在",null);
        }
        HospitalDTO hospital = hospitalAPI.info(info.getFUWUJGBH()).getData();
//        HospitalDTO hospital = dataMigrationAPI.getByHospitalId(info.getFUWUJGBH()).getData();
        if(Objects.isNull(hospital)){
            throw new CDAException("机构数据不存在",null);
        }

        ChanHouFS docData = new ChanHouFS();
        setClinicalDocumentData(docData, info, hospital);
        
        docData.setJianKangPG(createJianKangPG(info));
        docData.setJianKangWT(createJianKangWT(info));
        docData.setJianKangZD(createJianKangZD(info));
        docData.setRuXian(createRuXian(info));
        docData.setShengMingTZ(createShengMingTZ(info));
        docData.setShengZhiQi(createShengZhiQi(info));
        docData.setXiaCiSF(createXiaCiSF(info));
        docData.setZhuanZhenJY(createZhuanZhenJY(info));
        
        return docData;
    }
    
    private JianKangPG createJianKangPG(ChanHouCFFSVo info){
    	JianKangPG data = new JianKangPG();
    	data.setYiChangBZ("2".equals(info.getYUNCHANFPGYCDM())?"true":"false");
    	data.setYiChangMS(StringUtil.isNullOrEmpty(info.getYUNCHANFYCJG())?"-":info.getYUNCHANFYCJG());
    	return data;
    }
    
    private JianKangWT createJianKangWT(ChanHouCFFSVo info){
    	JianKangWT data = new JianKangWT();
    	data.setJianKangZTMS(info.getYIBANJKQK());
    	data.setXinLiZTMS(info.getYIBANXLQK());
    	return data;
    }
    
    private List<JianKangZD> createJianKangZD(ChanHouCFFSVo info){
    	List<JianKangZD> list = new ArrayList<JianKangZD>();
    	String[] dms = info.getBAOJIANZDLBDM().split("\\|");
    	for(int i=0;i<dms.length;i++){
    		JianKangZD data = new JianKangZD();
    		data.setZhiDao(new CodeObject(dms[i],info.getBAOJIANZDLBMC().split("\\|")[i]));
        	list.add(data);
    	}
    	return list;
    }
    
    private RuXian createRuXian(ChanHouCFFSVo info){
    	RuXian data = new RuXian();
        if("0".equals(info.getRUFANGYCDM()) || "3".equals(info.getRUFANGYCDM())){
            data.setZuoCe(new CodeObject("9","其他"));
            data.setYouCe(new CodeObject("9","其他"));
        } else if ("1".equals(info.getRUFANGYCDM()) ) {
            data.setZuoCe(new CodeObject("1","未见异常"));
            data.setYouCe(new CodeObject("1","未见异常"));
        }else if ("2".equals(info.getRUFANGYCDM()) ) {
            data.setZuoCe(new CodeObject("3","异常泌乳"));
            data.setYouCe(new CodeObject("3","异常泌乳"));
        }
    	return data;
    }
    
    private ShengMingTZ createShengMingTZ(ChanHouCFFSVo info){
    	ShengMingTZ data = new ShengMingTZ();
        if(info.getSHOUSUOY() !=null){
            data.setShouSuoYa(""+info.getSHOUSUOY());
        }
        if(info.getSHUZHANGY() !=null){
            data.setShuZhangYa(""+info.getSHUZHANGY());
        }

        if(info.getTIWEN() !=null){
            data.setTiWen(info.getTIWEN().setScale(1, BigDecimal.ROUND_HALF_UP).toString());
        }else{
            data.setTiWen("36.5");
        }

    	return data;
    }
    
    private ShengZhiQi createShengZhiQi(ChanHouCFFSVo info){
    	ShengZhiQi data = new ShengZhiQi();
    	data.setELuQK(StringUtil.isNullOrEmpty(info.getELUYCXS())?"-":info.getELUYCXS());
    	data.setELuYCBZ("2".equals(info.getELUYCDM())?"true":"false");
    	data.setGongTiQK(StringUtil.isNullOrEmpty(info.getZIGONGYCXS())?"-":info.getZIGONGYCXS());
    	data.setGongTiYCBZ("2".equals(info.getZIGONGYCDM())?"true":"false");
        if("1".equals(info.getSHANGKOUYCDM())){
            info.setSHANGKOUYCMC("愈合");
        } else if ("2".equals(info.getSHANGKOUYCDM())) {
            info.setSHANGKOUYCMC("未愈");
        } else if ("3".equals(info.getSHANGKOUYCDM())) {
            info.setSHANGKOUYCMC("感染");
        }else{
            info.setSHANGKOUYCMC("其他");
        }
    	data.setShangKouYH(new CodeObject(info.getSHANGKOUYCDM(),info.getSHANGKOUYCMC()));
    	return data;
    }
    
    private XiaCiSF createXiaCiSF(ChanHouCFFSVo info){
    	XiaCiSF data = new XiaCiSF();
    	data.setXiaCiSFSJ(CDAUtil.toD8(info.getXIACICHJCRQ()));
    	return data;
    }
    
    
    private FYZhuanZhenJY createZhuanZhenJY(ChanHouCFFSVo info){
    	FYZhuanZhenJY data = new FYZhuanZhenJY();
    	data.setZhuanZhenBZ("1".equals(info.getZHUANZHENBZ())?"true":"false");
    	data.setZhuanZhenYY(info.getZHUANZHENYY());
    	data.setZhuanRuJG(info.getZHUANRUJGMC());
    	data.setZhuanRuKS(info.getKESHIMC());
    	return data;
    }


    ClinicalDocumentData setClinicalDocumentData(ClinicalDocumentData clinicalDocumentData,ChanHouCFFSVo info, HospitalDTO hospital) {
        clinicalDocumentData.setEffectiveTime(CDAUtil.toD8(info.getCHANHOUFSRQ()));
        clinicalDocumentData.setPatientRole(createPatientRole(info));
        clinicalDocumentData.setAuthor(createAuthor(info, hospital));
        clinicalDocumentData.setCustodian(createCustodian(hospital));
        clinicalDocumentData.setId(String.valueOf(info.getId()));
        clinicalDocumentData.setVersionNumber("1.0");
        return clinicalDocumentData;
    }
    Custodian createCustodian(HospitalDTO hospital) {
        Custodian custodian = new Custodian();
        custodian.setId(hospital.getOutId());
        custodian.setName(hospital.getName());
        custodian.setAddr(hospital.getAddress());
        return custodian;
    }

    Author createAuthor(ChanHouCFFSVo info, HospitalDTO hospital) {
        Author author = new Author();
        author.setId("021");
        author.setTime(CDAUtil.toD8(info.getCHANHOUFSRQ()));
        author.setName(info.getCHANFUFSYSXM());
        author.setOrganizationId(hospital.getOutId());
        author.setOrganizationName(hospital.getName());
        author.setOrganizationAddr(hospital.getAddress());
        return author;
    }

    PatientRole createPatientRole(ChanHouCFFSVo info) {
        PatientRole patientRole = new PatientRole();
        patientRole.setId(CDAUtil.formatNumber(info.getGERENDABS()));
        patientRole.setPatient(createPatient(info));
        return patientRole;
    }


    Patient createPatient(ChanHouCFFSVo info) {
        Patient patient = new Patient();
        patient.setName(info.getXINGMING());
        patient.setId(info.getZHENGJIANHM());
        return patient;
    }

}
